package extend.org.compiere.hrm;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.LinkedHashMap;
import java.util.TimeZone;
import java.util.logging.Level;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.Years;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.Mhrmpayment;
import org.compiere.model.Mhrmsalaryemployee;
import org.compiere.model.Mhrmtax;
import org.compiere.model.MhrmtaxReserve;
import org.compiere.model.X_C_BPartner;
import org.compiere.model.X_hrm_catalog;
import org.compiere.model.X_hrm_salaryemployee;
import org.compiere.model.X_hrm_taxexemptions;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.eevolution.model.X_HR_Employee;

import extend.org.compiere.utils.Util;

/**
 * 
 * @author A.Yerzhanov
 * C_BPartner_ID 
 * SUM
 * Amount
 * dateFrom
 */

public class TaxCompute {
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (TaxCompute.class);
	
	public static TaxVariable GetTax(int C_BPartner_ID, double SUM, Timestamp dateFrom, String typetax){
		
		return TaxCompute.GetTax(C_BPartner_ID, SUM, dateFrom, typetax, "Y");
		
	} 
	
	public static TaxVariable GetTax(int C_BPartner_ID, double SUM, Timestamp dateFrom, String typetax, String closeTax) {
		
		Mhrmsalaryemployee mhrmsle = Mhrmsalaryemployee.getSalary(C_BPartner_ID, dateFrom);	
		X_C_BPartner bp = new X_C_BPartner(Env.getCtx(), C_BPartner_ID, null);
		if (mhrmsle == null){
			throw new AdempiereException (" For this person not set salary - " + bp.getName());
		}
			
		// set of employee benefits
		LinkedHashMap<String, Integer> taxList = getSetTaxExemptions(C_BPartner_ID);
		// 
		PaymentTax paymentType = new PaymentTax(typetax);	
		TaxVariable taxvar = new TaxVariable();
		Mhrmtax hrmtax = null;		
				
		/** Declare tax variable */
		TaxVariable taxValue = new TaxVariable(dateFrom);
		double taxMZP = taxValue.getMZP();
		double taxIPN = taxValue.getIPN();
		double taxOPV = taxValue.getOPV();		
		double taxCNalog = taxValue.getCNALOG();
		double taxCOtch = taxValue.getCOTCH();
		double MaxPension = taxValue.getMAXPENSION();
		
		/** Zero status  */
		double IPN = .0;
		double IPNTEMP = 0;
		double OPV = .0;
		double TOTAL = .0;
		double CNALOG = .0;
		double COTCH = .0;
		double TAXRELIEF = 0;
		double  PERSONAL = 0;
		boolean isDays = false;
		boolean isMin = false;
		double koef = 0.81;
		
		BigDecimal MAXIPN = new BigDecimal(0);
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
			
		if (SUM > .0){
			
			Timestamp edt = X_HR_Employee.getMaxEndDate(C_BPartner_ID, null);
			X_HR_Employee empl = X_HR_Employee.getHR_Employee(C_BPartner_ID, true);
			/*Timestamp bd = empl.gethrm_dateOfBirth();
			LocalDate birthdate = new LocalDate (bd);
			LocalDate now = new LocalDate(dateFrom);
			Years age = Years.yearsBetween(birthdate, now);
			if (age.getYears()>62) {
				taxOPV=0;
				taxCOtch=0;
			}*/
			if (C_BPartner_ID==2000048) {
				//
				koef=0.9;
			    PreparedStatement pstmt = null;
				ResultSet rs = null;
				LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
				Calendar FirstDay = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
				FirstDay.setTimeInMillis(dateFrom.getTime());
				FirstDay.set(Calendar.DAY_OF_MONTH,0);
				int iTID=0;
				// 
				String sql = "SELECT HRM_Tax_ID FROM HRM_Tax " +
								"WHERE ISACTIVE='Y' AND C_BPARTNER_ID = " + C_BPartner_ID+
								" AND DateFrom >="+DB.TO_DATE(new Timestamp(FirstDay.getTimeInMillis()), true)+
								" AND HRM_TypeTax <> '1000001'";
				try
				{
					pstmt = DB.prepareStatement(sql,null);			
					rs = pstmt.executeQuery();	
					while(rs.next()) {
						// key-HRM_TAXEXEMPTIONS_ID, value-Rate
						iTID = rs.getInt(1);
						if (iTID>0) {
							Mhrmtax t1 = new Mhrmtax(Env.getCtx(),iTID,null);
							if (!((t1.gethrm_OPV().equals(Env.ZERO)) 
							   &&(t1.gethrm_COtch().equals(Env.ZERO))))
							{
							  BigDecimal salary = t1.gethrm_total().subtract(t1.gethrm_OPV()).subtract(t1.gethrm_IPN());
							  BigDecimal totalsalary = salary.divide(new BigDecimal(0.9),20, BigDecimal.ROUND_HALF_DOWN);
							  BigDecimal bIPN = totalsalary.divide(new BigDecimal(10),20, BigDecimal.ROUND_HALF_DOWN);
							  BigDecimal bCNALOG = totalsalary.multiply(new BigDecimal(0.11));
							  t1.sethrm_OPV(Env.ZERO);
							  t1.sethrm_IPN(bIPN);
							  t1.sethrm_COtch(Env.ZERO);
							  t1.sethrm_CNalog(bCNALOG);
							  t1.sethrm_cnalogfinal(bCNALOG);
							  t1.sethrm_total(totalsalary);
							  t1.save();
							}  
						}
					}	
				}
				catch (SQLException e)
				{
					s_log.log(Level.SEVERE, "HRM_Tax ", e);
				}
				finally
				{
					DB.close(rs, pstmt);
					rs = null; pstmt = null;
				}	
				taxOPV=0;
				taxCOtch=0;
			}
			
			if (dateFrom.after(edt)){
				isDays = Util.is15Days(C_BPartner_ID, dateFrom);
			}
			
			boolean relief = Mhrmtax.isRelief(C_BPartner_ID, dateFrom);
			//
			if(new DateTime(dateFrom.getTime()).getDayOfMonth() <= 1)
				calendar.setTime(new DateTime(dateFrom.getTime()).plusDays(5).toDate());
			else
				calendar.setTimeInMillis(dateFrom.getTime());
			calendar.add(Calendar.MONTH, 1);
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			Timestamp dt = new Timestamp(calendar.getTimeInMillis());
			MhrmtaxReserve taxRNext = MhrmtaxReserve.getTaxReserve(C_BPartner_ID, dt);
			if(new DateTime(dateFrom.getTime()).getDayOfMonth() <= 1)
				calendar.setTime(new DateTime(dateFrom.getTime()).plusDays(5).toDate());
			else
				calendar.setTimeInMillis(dateFrom.getTime());
			calendar.set(Calendar.DAY_OF_MONTH, 0);
			MhrmtaxReserve taxRCurrent = MhrmtaxReserve.getTaxReserve(C_BPartner_ID, dateFrom);
			
			/** Fill hrm_tax table */
			if (typetax.equals(X_hrm_catalog.HRM_PAYMENTTYPE_BONUS)){
				hrmtax = Mhrmtax.getTaxType(C_BPartner_ID, dateFrom, typetax);
			}else{
				hrmtax = Mhrmtax.getTax(C_BPartner_ID, dateFrom, typetax);
			}
			
			
			if(hrmtax.get_ID() !=0 
					&& taxRNext != null 
					&& taxList.containsKey(X_hrm_taxexemptions.HRM_TAXTYPE_1MZP)
					&& paymentType.isRelief){
				taxRNext.sethrm_reserveMzp(taxRNext.gethrm_reserveMzp().add(hrmtax.gethrm_relief()));
				hrmtax.sethrm_relief(new BigDecimal(0));
				hrmtax.setisRelief(false);
				/*if (age.getYears()>62) {
					hrmtax.sethrm_OPV(Env.ZERO);
					hrmtax.sethrm_COtch(Env.ZERO);
				}*/
				taxRNext.saveEx();
				hrmtax.saveEx();			
			}
				
			if(hrmtax.isRelief() && taxList.containsKey(X_hrm_taxexemptions.HRM_TAXTYPE_1MZP)
					|| !isDays)
				TAXRELIEF = hrmtax.gethrm_relief().doubleValue();
			else
				TAXRELIEF = 0.;
			
			// Define tax relief : 1. get type, 2. take rate
			if (taxList.containsKey(X_hrm_taxexemptions.HRM_TAXTYPE_1MZP) 
					&& paymentType.isRelief 
					&& TAXRELIEF == 0.
					&& !hrmtax.isRelief()){
				
				if(taxRCurrent == null){
					taxRCurrent = new MhrmtaxReserve(Env.getCtx(), 0, null);
					taxRCurrent.setC_BPartner_ID(C_BPartner_ID);
					taxRCurrent.setDateFrom(new Timestamp(calendar.getTimeInMillis()));
					taxRCurrent.sethrm_reserveMzp(new BigDecimal(0));
					taxRCurrent.saveEx();
				}else if(new DateTime(dateFrom.getTime()).getMonthOfYear() == 1 
						&& hrmtax.isRelief()){
					taxRCurrent.sethrm_reserveMzp(new BigDecimal(0));
					taxRCurrent.saveEx();									
				}
				
				if(taxRNext == null){
					taxRNext = new MhrmtaxReserve(Env.getCtx(), 0, null);	
					if (!isDays){
						TAXRELIEF = taxMZP * taxList.get(X_hrm_taxexemptions.HRM_TAXTYPE_1MZP) + taxRCurrent.gethrm_reserveMzp().doubleValue();						
					}else{
						TAXRELIEF = 0;
					}
					taxRNext.setC_BPartner_ID(C_BPartner_ID);
					taxRNext.setDateFrom(dt);
					taxRNext.sethrm_reserveMzp(new BigDecimal(TAXRELIEF));
					taxRNext.saveEx();
				}else{
					if (relief){
						TAXRELIEF = taxRNext.gethrm_reserveMzp().doubleValue();
					}else if (!isDays){
						TAXRELIEF = taxRCurrent.gethrm_reserveMzp().doubleValue() + (taxMZP * taxList.get(X_hrm_taxexemptions.HRM_TAXTYPE_1MZP));
						taxRNext.sethrm_reserveMzp(new BigDecimal(TAXRELIEF)); 
						taxRNext.saveEx();
					}else{
						TAXRELIEF = 0;
					}					
				}
			}
			
			if (taxList.containsKey(X_hrm_taxexemptions.HRM_TAXTYPE_55MZP)){
					PERSONAL = taxMZP * taxList.get(X_hrm_taxexemptions.HRM_TAXTYPE_55MZP);	
					MAXIPN = Mhrmtax.getIPNofYear(C_BPartner_ID, taxList.get(X_hrm_taxexemptions.HRM_TAXTYPE_55MZP), dateFrom);
			}

			if ((C_BPartner_ID==2000025)&&(dateFrom.equals(Timestamp.valueOf("2013-09-30 00:00:00.0")) )) {
				TAXRELIEF=5714.29;
				taxRNext.sethrm_reserveMzp(new BigDecimal("5714.29"));
				taxRNext.saveEx();
			}
			
			/** Definition direct (2) or inverse (1 - default) payroll methods */	
			if (typetax.equals(TypePayment.VACATIONAID)){
				if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())){
					TOTAL = SUM;
					}else{
						if (PERSONAL >= (SUM - taxMZP)){
			    		TOTAL = (SUM - (TAXRELIEF * 0.1) - (PERSONAL * 0.1))/koef;
			    	}else if (MAXIPN.doubleValue() < PERSONAL 
			    			|| (SUM-SUM*taxOPV) < TAXRELIEF){
			    		TOTAL = SUM/0.9;
			    	}else{
			    		TOTAL = (SUM - TAXRELIEF * 0.1)/koef;
			    	}					
				}
			}else if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype()) 
					|| typetax.equals(TypePayment.MEDICARE)
					|| typetax.equals(TypePayment.PROFIT)
					|| typetax.equals(TypePayment.VACATION)
					|| typetax.equals(TypePayment.ILLNESS)
					|| typetax.equals(TypePayment.BUSINESSTRIP)					
					|| typetax.equals(TypePayment.DISMISSAL)){
		    	TOTAL = SUM;
		    	// The method of direct payroll				    	
		    }else{
		    	if (PERSONAL >= (SUM - taxMZP)){
		    		TOTAL = (SUM - (TAXRELIEF * 0.1) - (PERSONAL * 0.1))/koef;
		    	}else if (MAXIPN.doubleValue() < PERSONAL 
		    			|| (SUM-SUM*taxOPV) < TAXRELIEF){
		    		TOTAL = SUM/0.9;
		    	}else{
		    		TOTAL = (SUM - TAXRELIEF * 0.1)/koef;
		    	}
	    	// method of inverse calculation of wages
		    }
						
			boolean flag = false;
	
			// Pension calculate begin
			if(paymentType.isOpv){				
				//
				BigDecimal totalOPV = Mhrmtax.getTotalOPV(C_BPartner_ID, dateFrom); 
						//Mhrmpayment.getSalarySum_OPV(C_BPartner_ID, dateFrom);			
				MaxPension = MaxPension * taxMZP * taxOPV;
				
				if(totalOPV.doubleValue() == MaxPension)
					flag = true;
				
				//--add by A.K.
				if (typetax.equals(TypePayment.VACATION)||typetax.equals(TypePayment.VACATIONAID)){
					OPV = hrmtax.gethrm_OPV().doubleValue();
					totalOPV = totalOPV.add(new BigDecimal(OPV).negate());
					Mhrmtax hrmtax1 = Mhrmtax.getTax(C_BPartner_ID, dateFrom, TypePayment.VACATIONAID);
					OPV = hrmtax1.gethrm_OPV().doubleValue();
					totalOPV = totalOPV.add(new BigDecimal(OPV).negate());
					if(totalOPV.compareTo(new BigDecimal(1)) ==-1) 
						totalOPV = new BigDecimal(0);   
				}
				
				//--
				
				if (totalOPV.doubleValue() < MaxPension && !flag){
					//-----A.K.
					if((totalOPV.doubleValue() > 0) && (typetax.equals(TypePayment.VACATION)&&typetax.equals(TypePayment.VACATIONAID)))
						OPV = MaxPension - totalOPV.doubleValue();
					else					
						OPV = TOTAL * taxOPV;
					//----
					if (OPV > MaxPension){
						if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())){
							TOTAL = SUM;
						}else{
							TOTAL = (SUM - TAXRELIEF)/0.9 + MaxPension;
						}
						OPV = MaxPension;
					}else if (OPV < 0 
							|| totalOPV.doubleValue() > MaxPension){
						OPV = 0;
					}					
				}else{
					OPV = 0;
				}
			}
			
			// Pension calculate end 
			if(!hrmtax.isRelief()
					&& taxList.containsKey(X_hrm_taxexemptions.HRM_TAXTYPE_1MZP)
					&& paymentType.isRelief){
				if(taxRNext.gethrm_reserveMzp().doubleValue() >= TOTAL-OPV){
					isMin = true;
					taxRNext.sethrm_reserveMzp(new BigDecimal(TAXRELIEF-(TOTAL-OPV)));					
					taxRNext.saveEx();
					TAXRELIEF = TOTAL-OPV;
					taxRCurrent.sethrm_reserveMzp(new BigDecimal(TAXRELIEF));
					taxRCurrent.saveEx();
				}else{
					if (!isDays 
							&& !relief){
						TAXRELIEF = taxRNext.gethrm_reserveMzp().doubleValue();
					}					
				}				
			}
				// IPN tax calculate begin			
				if(paymentType.isIpn){		
					
					if(isDays){
						TAXRELIEF = 0.;
					}						
					
					if (MAXIPN.doubleValue() < PERSONAL){
							IPNTEMP = 0;
							IPN =  (((SUM - TAXRELIEF * 0.1)/0.81) - ((SUM - TAXRELIEF * 0.1)/0.81)*0.1 - TAXRELIEF) * taxIPN;
						}else{
							IPNTEMP = (TOTAL - OPV - TAXRELIEF) * taxIPN;
							IPN =  (TOTAL - OPV - TAXRELIEF) * taxIPN;
						}				
					
					if (IPN < 0)
						IPN = 0;
				}
				// 	IPN tax calculate end

				//
				BigDecimal total = Mhrmpayment.getSalarySum_Result(C_BPartner_ID, dateFrom);
				if(total.doubleValue() == 0)
					total = new BigDecimal(TOTAL);
	
				// Calculate social insurance begin
				if(paymentType.isCotch){
					if (total.doubleValue() <= taxMZP){  // TODO вернуться после тестирования. V.Sokolov add = < ...&& IPN != 0... >
					 COTCH = taxMZP * taxCOtch; // c 17.02.2013
					// COTCH = (taxMZP - taxMZP * taxOPV) * taxCOtch;
					}else 
					if(((TOTAL - OPV) * taxCOtch) > (10 * taxMZP * taxCOtch)){			
						COTCH = 10 * taxMZP * taxCOtch;
					}else{
						COTCH = (TOTAL - OPV) * taxCOtch;
					}
					
					if (COTCH < 0){
						COTCH = 0;
					}
				}
				// Calculate social insurance end
				
				// Calculate social taxes
				if(paymentType.isCnalog){
					if (total.doubleValue() <= taxMZP) 
					{
						CNALOG = taxMZP * taxCNalog;
					}else{
						CNALOG = (TOTAL - OPV) * taxCNalog;	
					}	
	
					if (CNALOG < .0){
						CNALOG = 0;		
					}
				}
				// Calculate social taxes end			
				
			if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())
					&& (typetax.equals(TypePayment.VACATIONAID))){
				TOTAL = SUM;
			}else if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())
					&& (typetax.equals(TypePayment.BONUS))){
				TOTAL = SUM;
			}else{
				TOTAL = TOTAL - (OPV + IPNTEMP);
			}
			
			if (TOTAL > 0){				
				if(typetax.equals(TypePayment.DISMISSAL)){
					TOTAL = SUM;
					OPV = 0.;
					CNALOG = 0.;
					COTCH = 0.;
				}
				
				hrmtax.setC_BPartner_ID(C_BPartner_ID);
				hrmtax.setDateFrom(dateFrom);
				if (typetax.equals(TypePayment.REVIEW)){
					hrmtax.sethrm_OPV(new BigDecimal(OPV).negate());
					hrmtax.sethrm_IPN(new BigDecimal(IPN).negate());
					hrmtax.sethrm_CNalog(new BigDecimal(CNALOG).negate());
					hrmtax.sethrm_COtch(new BigDecimal(0));		
					hrmtax.sethrm_cnalogfinal(new BigDecimal(CNALOG).negate());
				}else{
					hrmtax.sethrm_IPN(new BigDecimal(IPN));
					hrmtax.sethrm_OPV(new BigDecimal(OPV));					
					hrmtax.sethrm_CNalog(new BigDecimal(CNALOG));
					hrmtax.sethrm_COtch(new BigDecimal(COTCH));		
					hrmtax.sethrm_cnalogfinal(new BigDecimal(CNALOG-COTCH));
				}
				hrmtax.sethrm_typetax(typetax);
				if (typetax.equals(TypePayment.VACATIONAID)){
					hrmtax.sethrm_total(new BigDecimal(TOTAL));
				}else if (X_hrm_salaryemployee.HRM_SALARYTYPE_Direct.equals(mhrmsle.gethrm_salarytype())
						&& (typetax.equals(TypePayment.BONUS))){
					hrmtax.sethrm_total(new BigDecimal(TOTAL));
				}else if (typetax.equals(TypePayment.REVIEW)){
					hrmtax.sethrm_total(new BigDecimal(TOTAL + IPN + OPV).negate());
				}else{
					hrmtax.sethrm_total(new BigDecimal(TOTAL + IPN + OPV));
				}
				hrmtax.sethrm_relief(new BigDecimal(TAXRELIEF));
				if (TAXRELIEF == 0){
					hrmtax.setisRelief(false);					
				}else if (isMin){
					hrmtax.setisRelief(true);
				}else{
					hrmtax.setisRelief(true);
					if(taxRNext!=null){ //TODO A.Kulantayev  нужно понять логику участия в процесе и инициализации объекта taxRNext (пока что временный кастыль от 25.07.2013 10:12)
					taxRNext.sethrm_reserveMzp(taxRNext.gethrm_reserveMzp().add(new BigDecimal(TAXRELIEF).negate()));																				
					taxRNext.saveEx();
					}
				}			
				
				hrmtax.saveEx();
				
			}else{
				IPN = 0;
				OPV = 0;
				TOTAL = SUM;
				CNALOG = 0;
				COTCH = 0;
			}			 
		}else{
			IPN = 0;
			OPV = 0;
			TOTAL = 0;
			CNALOG = 0;
			COTCH = 0;
			SUM = 0;			   
		}
	
		taxvar.setIPN(IPNTEMP);
		taxvar.setOPV(OPV);
		taxvar.setCNALOG(CNALOG);
		taxvar.setCOTCH(COTCH);
		taxvar.setTOTAL(TOTAL);
		taxvar.setSUM(SUM);
		
		
		return taxvar;
	}
	
	public static LinkedHashMap<String, Integer> getSetTaxExemptions(int C_BPartner_ID){
		
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		LinkedHashMap<String, Integer> map = new LinkedHashMap<String, Integer>();
			
		// 
		String sql_ = "SELECT HRM_TAXTYPE, RATE FROM HRM_TAXEXEMPTIONSEMPLOYEE " +
						"WHERE ISACTIVE='Y' AND C_BPARTNER_ID = " + C_BPartner_ID;
		try
		{
			pstmt = DB.prepareStatement(sql_,null);			
			rs = pstmt.executeQuery();	
			while(rs.next())
				// key-HRM_TAXEXEMPTIONS_ID, value-Rate
				map.put(rs.getString(1), rs.getInt(2));
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		return map;
	}	
	
	public static TaxVariable getTypeTax(int C_BPartner, Timestamp dateFrom, String typeTax){
		
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		TaxVariable value = new TaxVariable();
		
		// 
		String sql_ = "SELECT SUM(HRM_OPV) as HRM_OPV, " +
				"			  SUM(HRM_IPN) as HRM_IPN, " +
				"			  SUM(HRM_COTCH) as HRM_COTCH, " +
				"		      SUM(HRM_CNALOG) as HRM_CNALOG FROM HRM_TAX WHERE C_BPARTNER_ID="+C_BPartner+" \n" +
				"AND TO_CHAR(DATEFROM, 'MM-YYYY') = TO_CHAR("+DB.TO_DATE(dateFrom, true)+",'MM-YYYY') \n" +
				"AND HRM_TYPETAX='"+typeTax+"'";
		try
		{
			pstmt = DB.prepareStatement(sql_,null);			
			rs = pstmt.executeQuery();	
			if(rs.next()){
				value.setOPV(rs.getDouble("hrm_opv"));
				value.setIPN(rs.getDouble("hrm_ipn"));
				value.setCOTCH(rs.getDouble("hrm_cotch"));
				value.setCNALOG(rs.getDouble("hrm_cnalog"));
			}
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		
		return value;
	}	
	
	public static class PaymentTax{
		
		private boolean isOpv;
		private boolean isIpn;
		private boolean isCnalog;
		private boolean isCotch;
		private boolean isAvg;
		private boolean isTotal;
		private boolean isRelief;
		
		private String typePayment = "";
		
		PaymentTax(){
			//
		}
		
		public PaymentTax(String type){
			typePayment=type;
			getCatalog();
		}
		
		public boolean isOpv() {
			return isOpv;
		}

		public void setOpv(boolean isOpv) {
			this.isOpv = isOpv;
		}

		public boolean isIpn() {
			return isIpn;
		}

		public void setIpn(boolean isIpn) {
			this.isIpn = isIpn;
		}

		public boolean isCnalog() {
			return isCnalog;
		}

		public void setCnalog(boolean isCnalog) {
			this.isCnalog = isCnalog;
		}

		public boolean isCotch() {
			return isCotch;
		}

		public void setCotch(boolean isCotch) {
			this.isCotch = isCotch;
		}

		public boolean isAvg() {
			return isAvg;
		}

		public void setAvg(boolean isAvg) {
			this.isAvg = isAvg;
		}

		public boolean isTotal() {
			return isTotal;
		}

		public void setTotal(boolean isTotal) {
			this.isTotal = isTotal;
		}

		public boolean isRelief() {
			return isRelief;
		}
		
		public void setRelief(boolean isRelief) {
			this.isRelief = isRelief;
		}

		private void getCatalog(){
			
			//
		    PreparedStatement pstmt = null;
			ResultSet rs = null;
			
			// 
			String sql_ = "SELECT * FROM HRM_CATALOG WHERE HRM_PAYMENTTYPE='"+typePayment+"'";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);			
				rs = pstmt.executeQuery();	
				if(rs.next()){
					setOpv(rs.getString("hrm_isopv").equals("Y"));
					setIpn(rs.getString("hrm_isipn").equals("Y"));
					setCnalog(rs.getString("hrm_iscnalog").equals("Y"));
					setCotch(rs.getString("hrm_iscotch").equals("Y"));
					setAvg(rs.getString("hrm_isaverage").equals("Y"));
					setTotal(rs.getString("hrm_istotal").equals("Y"));
					setRelief(rs.getString("hrm_isrelief").equals("Y"));
				}
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}

		}	
		
	}
	
}